package lct.fontcompiler;

import lct.util.PathUtility;
import lct.util.XMLUtility;

import java.util.Vector;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class XMLConverter {
	public XMLConverter() {		
	}
	
	public void storeManifest(Manifest manifest, String filePath) {
		try {
			String directoryPath = PathUtility.getDirectoryFull(filePath);
			
			Document document = XMLUtility.createDocument();
			
			Element manifestElement = document.createElement("fontconverter:manifest");
			{
				Element sheetEntriesElement = document.createElement("sheetEntries");
				for (Manifest.SheetEntry sheetEntry : manifest.sheetEntryVector) {
					Element sheetEntryElement = document.createElement("sheetEntry");
					{
						String sheetRelativePath = PathUtility.makeRelative(directoryPath, sheetEntry.filePath);
						XMLUtility.writeStringElement(document, "filePath", sheetRelativePath, sheetEntryElement);
					}
					sheetEntriesElement.appendChild(sheetEntryElement);
				}
				manifestElement.appendChild(sheetEntriesElement);
				
				Element textureEntriesElement = document.createElement("textureEntries");
				for (Manifest.TextureEntry textureEntry : manifest.textureEntryVector) {
					Element textureEntryElement = document.createElement("textureEntry");
					{
						String textureRelativePath = PathUtility.makeRelative(directoryPath, textureEntry.filePath);
						XMLUtility.writeStringElement(document, "filePath", textureRelativePath, textureEntryElement);
					}
					textureEntriesElement.appendChild(textureEntryElement);
				}
				manifestElement.appendChild(textureEntriesElement);
			}
			document.appendChild(manifestElement);
			
			XMLUtility.storeDocument(document, filePath);
		}
		catch (Exception e) {
			System.out.println(e.getMessage());
			System.out.println(e.getStackTrace());
		}
	}
	
	public Manifest loadManifest(String filePath) {
		try {
			String directoryPath = PathUtility.getDirectoryFull(filePath);
			
			Document document = XMLUtility.loadDocument(filePath);
			
			Element manifestElement = (Element)document.getElementsByTagName("fontconverter:manifest").item(0);
			Manifest manifest = new Manifest();
			{
				Element sheetEntriesElement = (Element)manifestElement.getElementsByTagName("sheetEntries").item(0);
				NodeList sheetEntryNodeList = sheetEntriesElement.getElementsByTagName("sheetEntry");
				manifest.sheetEntryVector = new Vector<Manifest.SheetEntry>();
				for (int sheetEntryIndex = 0; sheetEntryIndex < sheetEntryNodeList.getLength(); ++sheetEntryIndex) {
					Element sheetEntryElement = (Element)sheetEntryNodeList.item(sheetEntryIndex);
					Manifest.SheetEntry sheetEntry = new Manifest.SheetEntry();
					{
						String sheetRelativeFilePath = XMLUtility.readStringElement(sheetEntryElement, "filePath", 0);
						sheetEntry.filePath = PathUtility.combineFull(directoryPath, sheetRelativeFilePath);
					}
					manifest.sheetEntryVector.add(sheetEntry);
				}
				
				Element textureEntriesElement = (Element)manifestElement.getElementsByTagName("textureEntries").item(0);
				NodeList textureEntryNodeList = textureEntriesElement.getElementsByTagName("textureEntry");
				manifest.textureEntryVector = new Vector<Manifest.TextureEntry>();
				for (int textureEntryIndex = 0; textureEntryIndex < textureEntryNodeList.getLength(); ++textureEntryIndex) {
					Element textureEntryElement = (Element)textureEntryNodeList.item(textureEntryIndex);
					Manifest.TextureEntry textureEntry = new Manifest.TextureEntry();
					{
						String textureRelativeFilePath = XMLUtility.readStringElement(textureEntryElement, "filePath", 0);
						textureEntry.filePath = PathUtility.combineFull(directoryPath, textureRelativeFilePath);
					}
					manifest.textureEntryVector.add(textureEntry);
				}
			}
			return manifest;
		}
		catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
			
			return null;
		}
	}
}
